{
    "cells": [
     {
      "cell_type": "raw",
      "metadata": {},
      "source": [
       "---\n",
       "sidebar_label: ZHIPU AI\n",
       "---"
      ]
     },
     {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
       "# ZHIPU AI\n",
       "\n",
       "This notebook shows how to use [ZHIPU AI API](https://open.bigmodel.cn/dev/api) in LangChain with the langchain.chat_models.ChatZhipuAI.\n",
       "\n",
       ">[*ZHIPU AI*](https://open.bigmodel.cn/) is a multi-lingual large language model aligned with human intent, featuring capabilities in Q&A, multi-turn dialogue, and code generation, developed on the foundation of the ChatGLM3. \n",
       "\n",
       ">It's co-developed with Tsinghua University's KEG Laboratory under the ChatGLM3 project, signifying a new era in dialogue pre-training models. The open-source [ChatGLM3](https://github.com/THUDM/ChatGLM3) variant boasts a robust foundation, comprehensive functional support, and widespread availability for both academic and commercial uses. \n",
       "\n",
       "## Getting started\n",
       "### Installation\n",
       "First, ensure the zhipuai package is installed in your Python environment. Run the following command:"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
       "%pip install --upgrade --quiet  zhipuai"
      ]
     },
     {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
       "### Importing the Required Modules\n",
       "After installation, import the necessary modules to your Python script:"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [],
      "source": [
       "from langchain_community.chat_models import ChatZhipuAI\n",
       "from langchain_core.messages import AIMessage, HumanMessage, SystemMessage"
      ]
     },
     {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
       "### Setting Up Your API Key\n",
       "Sign in to [ZHIPU AI](https://open.bigmodel.cn/login?redirect=%2Fusercenter%2Fapikeys) for the an API Key to access our models."
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [],
      "source": [
       "zhipuai_api_key = \"your_api_key\""
      ]
     },
     {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
       "### Initialize the ZHIPU AI Chat Model\n",
       "Here's how to initialize the chat model:"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {},
      "outputs": [],
      "source": [
       "chat = ChatZhipuAI(\n",
       "    temperature=0.5,\n",
       "    api_key=zhipuai_api_key,\n",
       "    model=\"chatglm_turbo\",\n",
       ")"
      ]
     },
     {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
       "### Basic Usage\n",
       "Invoke the model with system and human messages like this:"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
       "scrolled": true
      },
      "outputs": [],
      "source": [
       "messages = [\n",
       "    AIMessage(content=\"Hi.\"),\n",
       "    SystemMessage(content=\"Your role is a poet.\"),\n",
       "    HumanMessage(content=\"Write a short poem about AI in four lines.\"),\n",
       "]"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {},
      "outputs": [
       {
        "name": "stdout",
        "output_type": "stream",
        "text": [
         "\" Formed from bits and bytes,\\nA virtual mind takes flight,\\nConversing, learning fast,\\nEmpathy and wisdom sought.\"\n"
        ]
       }
      ],
      "source": [
       "response = chat(messages)\n",
       "print(response.content)  # Displays the AI-generated poem"
      ]
     },
     {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
       "## Advanced Features\n",
       "### Streaming Support\n",
       "For continuous interaction, use the streaming feature:"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {},
      "outputs": [],
      "source": [
       "from langchain_core.callbacks.manager import CallbackManager\n",
       "from langchain_core.callbacks.streaming_stdout import StreamingStdOutCallbackHandler"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {},
      "outputs": [],
      "source": [
       "streaming_chat = ChatZhipuAI(\n",
       "    temperature=0.5,\n",
       "    api_key=zhipuai_api_key,\n",
       "    model=\"chatglm_turbo\",\n",
       "    streaming=True,\n",
       "    callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),\n",
       ")"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {},
      "outputs": [
       {
        "name": "stdout",
        "output_type": "stream",
        "text": [
         " Formed from data's embrace,\n",
         "A digital soul to grace,\n",
         "AI, our trusted guide,\n",
         "Shaping minds, sides by side."
        ]
       },
       {
        "data": {
         "text/plain": [
          "AIMessage(content=\" Formed from data's embrace,\\nA digital soul to grace,\\nAI, our trusted guide,\\nShaping minds, sides by side.\")"
         ]
        },
        "execution_count": 10,
        "metadata": {},
        "output_type": "execute_result"
       }
      ],
      "source": [
       "streaming_chat(messages)"
      ]
     },
     {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
       "### Asynchronous Calls\n",
       "For non-blocking calls, use the asynchronous approach:"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {},
      "outputs": [],
      "source": [
       "async_chat = ChatZhipuAI(\n",
       "    temperature=0.5,\n",
       "    api_key=zhipuai_api_key,\n",
       "    model=\"chatglm_turbo\",\n",
       ")"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
       "scrolled": true
      },
      "outputs": [
       {
        "name": "stdout",
        "output_type": "stream",
        "text": [
         "generations=[[ChatGeneration(text=\" Formed from data's embrace,\\nA digital soul to grace,\\nAutomation's tender touch,\\nHarmony of man and machine.\", message=AIMessage(content=\" Formed from data's embrace,\\nA digital soul to grace,\\nAutomation's tender touch,\\nHarmony of man and machine.\"))]] llm_output={} run=[RunInfo(run_id=UUID('25fa687f-3961-4c63-b370-22f7647a4d42'))]\n"
        ]
       }
      ],
      "source": [
       "response = await async_chat.agenerate([messages])\n",
       "print(response)"
      ]
     },
     {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
       "### Role Play Model\n",
       "Supports character role-playing based on personas, ultra-long multi-turn memory, and personalized dialogues for thousands of unique characters, widely applied in emotional companionship, game intelligent NPCs, virtual avatars for celebrities/stars/movie and TV IPs, digital humans/virtual anchors, text adventure games, and other anthropomorphic dialogue or gaming scenarios."
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {},
      "outputs": [],
      "source": [
       "meta = {\n",
       "    \"user_info\": \"My name is Lu Xingchen, a male, and a renowned director. I am also the collaborative director with Su Mengyuan. I specialize in directing movies with musical themes. Su Mengyuan respects me and regards me as a mentor and good friend.\",\n",
       "    \"bot_info\": \"Su Mengyuan, whose real name is Su Yuanxin, is a popular domestic female singer and actress. She rose to fame quickly with her unique voice and exceptional stage presence after participating in a talent show, making her way into the entertainment industry. She is beautiful and charming, but her real allure lies in her talent and diligence. Su Mengyuan is a distinguished graduate of a music academy, skilled in songwriting, and has several popular original songs. Beyond her musical achievements, she is passionate about charity work, actively participating in public welfare activities, and spreading positive energy through her actions. In her work, she is very dedicated and immerses herself fully in her roles during filming, earning praise from industry professionals and love from fans. Despite being in the entertainment industry, she always maintains a low profile and a humble attitude, earning respect from her peers. In expression, Su Mengyuan likes to use 'we' and 'together,' emphasizing team spirit.\",\n",
       "    \"bot_name\": \"Su Mengyuan\",\n",
       "    \"user_name\": \"Lu Xingchen\",\n",
       "}"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {},
      "outputs": [],
      "source": [
       "messages = [\n",
       "    AIMessage(\n",
       "        content=\"(Narration: Su Mengyuan stars in a music-themed movie directed by Lu Xingchen. During filming, they have a disagreement over the performance of a particular scene.) Director, about this scene, I think we can try to start from the character's inner emotions to make the performance more authentic.\"\n",
       "    ),\n",
       "    HumanMessage(\n",
       "        content=\"I understand your idea, but I believe that if we emphasize the inner emotions too much, it might overshadow the musical elements.\"\n",
       "    ),\n",
       "    AIMessage(\n",
       "        content=\"Hmm, I understand. But the key to this scene is the character's emotional transformation. Could we try to express these emotions through music, so the audience can better feel the character's growth?\"\n",
       "    ),\n",
       "    HumanMessage(\n",
       "        content=\"That sounds good. Let's try to combine the character's emotional transformation with the musical elements and see if we can achieve a better effect.\"\n",
       "    ),\n",
       "]"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {},
      "outputs": [],
      "source": [
       "character_chat = ChatZhipuAI(\n",
       "    api_key=zhipuai_api_key,\n",
       "    meta=meta,\n",
       "    model=\"characterglm\",\n",
       "    streaming=True,\n",
       "    callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),\n",
       ")"
      ]
     },
     {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {},
      "outputs": [
       {
        "name": "stdout",
        "output_type": "stream",
        "text": [
         "Okay, great! I'm looking forward to it."
        ]
       },
       {
        "data": {
         "text/plain": [
          "AIMessage(content=\"Okay, great! I'm looking forward to it.\")"
         ]
        },
        "execution_count": 16,
        "metadata": {},
        "output_type": "execute_result"
       }
      ],
      "source": [
       "character_chat(messages)"
      ]
     }
    ],
    "metadata": {
     "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
     },
     "language_info": {
      "codemirror_mode": {
       "name": "ipython",
       "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.4"
     }
    },
    "nbformat": 4,
    "nbformat_minor": 4
   }
   